package com.shuqi.common;

import java.util.Properties;

import com.shuqi.common.utils.Log4an;

import android.content.Context;
import android.text.TextUtils;

/**
 *  从资源文件下获取属性值
 *
 */
public class PropertiesUtils {
    public static final String SWITCH_ON = "1";
    public static final String SWITCH_OFF = "0";

    public static final String FILEPATH_DOMAIN = "config/domain.ini";
    public static final String FILEPATH_SWTCH = "config/switch.ini";

    public static final String PROPERTIE_SWTCH_OFFERWALL = "switch_offerwall";
    public static final String PROPERTIE_SWTCH_MACADD = "switch_macadd";

    /***
     * 获取推广墙的开关状态
     * @param context
     * @param defaultValue 默认值
     * @return
     */
    public static boolean isOfferwallSwitchLocalOpen(Context context, boolean defaultValue) {
        String value = getPropertiesString(context, FILEPATH_SWTCH, PROPERTIE_SWTCH_OFFERWALL);
        if (TextUtils.isEmpty(value)) {
            return defaultValue;
        } else {
            return SWITCH_ON.equals(value);
        }
    }
    /***
     * 获取MAC地址的开关状态
     * @param context
     * @param defaultValue 默认值
     * @return
     */
    public static boolean isMacAddSwitchLocalOpen(Context context, boolean defaultValue) {
        String value = getPropertiesString(context, FILEPATH_SWTCH, PROPERTIE_SWTCH_MACADD);
        if (TextUtils.isEmpty(value)) {
            return defaultValue;
        } else {
            return SWITCH_ON.equals(value);
        }
    }

    /***
     * 获取属性值
     * 
     * @param context
     * @param fileName
     * @param propertyName
     * @return
     */
    public static String getPropertiesString(Context context, String fileName, String propertyName) {
        try {
            Properties pro = new Properties();
            pro.load(context.getAssets().open(fileName));
            String value = pro.getProperty(propertyName);
            Log4an.i("PropertiesUtils", propertyName+": "+value);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
